<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\LogRoleStallDTO;
use App\Exceptions\ServiceException;
use App\Http\Controllers\Controller;
use App\Models\Game\Equip;
use App\Services\Game\LogRoleStallService;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;
use JsonSchema\Uri\Retrievers\AbstractRetriever;

class LogRoleStallController extends Controller
{
    private LogRoleStallService $logRoleStallService;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;
    private Request $request;
    /**
     * LogRoleStallController constructor.
     * @param Request $request
     * @param LogRoleStallService $logRoleStallService
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(Request $request,LogRoleStallService $logRoleStallService, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->request = $request;
        $this->logRoleStallService = $logRoleStallService;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }


    public function paginate(){
        try {
            global $gameArea;

            $searchData = $this->request->all();
            $memberName = data_get($searchData,'roleName');
            $equipName = data_get($searchData,'equipName');

            $result = callRpc("/LogStallService/paginate",$searchData,['gameArea'=>$gameArea]);
            $memIds = collect($result['data'])->pluck('memId')->unique()->toArray();
            $memIds = array_values($memIds);

            $members = $this->userMemberService->getByIds($memIds)->toArray();
            if ($memberName) {
                $result['data']= collect($result['data'])->where('nick', $memberName)->all();
            }
            $itemIds = collect($result['data'])->pluck('item_id')->unique()->toArray();
            $equips = Equip::query()->whereIn('id',$itemIds)->get()->toArray();
            if ($equipName) {
                $equips = collect($equips)->where('name', $equipName)->all();
            }

            foreach($result['data'] as &$datum) {
                $member = collect($members)->where('id',$datum['memId'])->first();
                $equip = collect($equips)->where('id',$datum['item_id'])->first();
                $datum['memberName'] = data_get($member,'username');
                $datum['equipName'] = data_get($equip,'name');
                $datum['actionTypeName'] = $this->actionTypeName($datum['action_type']);
                $datum['unique_id'] = (string)$datum['unique_id'];
            }
            $res = [];
            $res['code'] = 0;
            $res['data'] = $result['data'];
            $res['count'] = $result['count'];
            return ResponseUtil::json($res);
        }catch (ServiceException $e){
            return ResponseUtil::error($e->getMessage());
        }
    }


    private function actionTypeName($actionType){
        switch($actionType){
                    case 0: return '摆摊';
                    case 1: return '收摊';
                    case 2: return '上架';
                    case 3: return '下架';
                    case 4: return '购入';
                    case 5: return '售出';
                    case 6: return '提取';
                    case 7: return '存取货币';
                    default: return '';
            }
    }
}
